﻿using System;
using System.Collections.Specialized;

namespace Crocodoc
{
    /// <summary>
    /// Provides access to the Crocodoc Download API. The Download API is used for
    /// downloading an original of a document, a PDF of a document, a thumbnail of a
    /// document, and text extracted from a document.
    /// </summary>
    public class CrocodocDownload : Crocodoc
    {
        /// <summary>
        /// The download api path relative to the base API path.
        /// </summary>
        public static string _path = "download/";

        /// <summary>
        /// Gets or sets the Download API path relative to the base API path.
        /// </summary>
        public new static string Path
        {
            get
            {
                return _path;
            }
            set
            {
                _path = value;
            }
        }

        /// <summary>
        /// Download a document's original file from Crocodoc.
        /// </summary>
        /// <param name="uuid">The uuid of the file to download.</param>
        /// <returns>The document as a byte array.</returns>
        public static byte[] Document(string uuid)
        {
            return Document(uuid, null);
        }

        /// <summary>
        /// Download a document's original file from Crocodoc. The file can optionall be downloaded as a PDF, with annotations, and with filtered annotations.
        /// </summary>
        /// <param name="uuid">The uuid of the file to download.</param>
        /// <param name="options">The download options.</param>
        /// <returns>The document as a byte array.</returns>
        public static byte[] Document(string uuid, DownloadDocumentOptions options)
        {
            NameValueCollection getParams = new NameValueCollection();
            getParams.Add("uuid", uuid);

            if (options != null)
            {
                if (options.IsPDF)
                {
                    getParams.Add("pdf", "true");
                }
                if (options.IsAnnotated)
                {
                    getParams.Add("annotated", "true");
                }
                if (!String.IsNullOrEmpty(options.Filter))
                {
                    getParams.Add("filter", options.Filter);
                }
            }

            return GetData(Path, "document", getParams);
        }

        /// <summary>
        /// Download a document's extracted text from Crocodoc. Must be activated on the account to do this.
        /// </summary>
        /// <param name="uuid">The unique identifier of the document to get the text for.</param>
        /// <returns>The extracted text as a string.</returns>
        public static string Text(string uuid)
        {
            NameValueCollection getParams = new NameValueCollection();
            getParams.Add("uuid", uuid);

            return GetRequest<string>(Path, "text", getParams);
        }

        /// <summary>
        /// Download a document's thumbnail from Crocodoc with the default size (100x100).
        /// </summary>
        /// <param name="uuid">The uuid of the file to download the thumbnail from.</param>
        /// <returns></returns>
        public static byte[] Thumbnail(string uuid)
        {
            NameValueCollection getParams = new NameValueCollection();
            getParams.Add("uuid", uuid);

            return GetData(Path, "thumbnail", getParams);
        }

        /// <summary>
        /// Download a document's thumbnail from Crocodoc with an optional size.
        /// </summary>
        /// <param name="uuid">The uuid of the file to download the thumbnail from.</param>
        /// <param name="width">The width of the thumbnail</param>
        /// <param name="height">The height of the thumbnail</param>
        /// <returns></returns>
        public static byte[] Thumbnail(string uuid, int width, int height)
        {
            NameValueCollection getParams = new NameValueCollection();
            getParams.Add("uuid", uuid);
            getParams.Add("size", String.Format("{0}x{1}", width, height));

            return GetData(Path, "thumbnail", getParams);
        }
    }
}
